package com.itheima.chapter08.set.map.treemap;
//输入字符串，统计每个字符出现的次数 输出形式为a(2)b(3)...

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapdemo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String s = sc.nextLine();
        //无排序
        HashMap<Character, Integer> hm = new HashMap<>();
        //利用自然排序
        TreeMap<Character, Integer> tm = new TreeMap<>();
        for (int i = 0; i < s.length(); i++) {
            char key = s.charAt(i);
            Integer value = tm.get(key);
            if (value == null) {
                tm.put(key, 1);
            } else {
                value++;
                tm.put(key, value);
            }
        }

        StringBuilder sb = new StringBuilder();
        Set<Character> keySet = tm.keySet();
        for (Character key : keySet) {
            Integer value = tm.get(key);
            sb.append(key).append("(").append(value).append(")");
        }
        System.out.println(sb.toString());

    }
}
